package com.qq.financeSystem.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qq.financeSystem.model.EmpSalary;
import com.qq.financeSystem.model.YearAndMonth;
import com.qq.financeSystem.service.OffsetService;
import com.qq.financeSystem.service.SendSalaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/empSalary")
public class SendSalaryController {
    @Autowired
    SendSalaryService sendSalaryService;
    @Autowired
    OffsetService offsetService;

    //人员本月工资总览（根据部门识别码）
    @RequestMapping("/showNowAllSalary")
    public String showNowAllSalary(ModelMap m, @RequestParam(defaultValue = "1", value = "identificationCode") String identificationCode,
                                   @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum) {
        PageHelper.startPage(pageNum, 10);
        List<EmpSalary> empSalaries = sendSalaryService.queryAllSalary(identificationCode);
        PageInfo<EmpSalary> pageInfo = new PageInfo<>(empSalaries);
        m.addAttribute("pageInfo", pageInfo);
        //识别码判断部门
        m.addAttribute("ident", identificationCode);
        return "lsq/sendSalary/sendSalarySystem";
    }

    //指定历史人员工资总览(模态框，Id)
    @RequestMapping("/showLastOneSalary")
    @ResponseBody
    public List<EmpSalary> showLastOneSalary(String employeeId, ModelMap m) {
        return sendSalaryService.queryOneById(employeeId);
    }

    //跳历史查询页面
    @RequestMapping("/selectSalary")
    public String selectSalary() {
        return "redirect:showAllSalaryByKey";
    }

    //指定历史年月部门人员工资表查询（识别码，查询条件跟着走）
    @RequestMapping("/showAllSalaryByKey")
    public String showAllSalaryByKey(YearAndMonth key, ModelMap m, @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum) {
        PageHelper.startPage(pageNum, 10);
        List<EmpSalary> empSalaries = sendSalaryService.selectAllEmpSalaryByKey(key);
        PageInfo<EmpSalary> pageInfo = new PageInfo<>(empSalaries);
        m.addAttribute("pageInfo", pageInfo);
        //查询条件跟着页面走
        m.addAttribute("Key", key);
        return "lsq/sendSalary/selectSalarySystem";
    }

    //跳页面->本月个人详情薪资组成（识别码，姓名，拿数据）
    @RequestMapping("/showNowOneSalary")
    public String showNowOneSalary(String employeeId, String name, String ident, ModelMap m) {
        //姓名
        m.addAttribute("name", name);
        //识别码
        m.addAttribute("ident", ident);
        //本月汽车订单
        if ("1".equals(ident))
            m.addAttribute("cars", sendSalaryService.queryCarById(employeeId));
            //本月修理订单
        else if ("3".equals(ident))
            m.addAttribute("services", sendSalaryService.queryServiceById(employeeId));
        //本月报销补贴
        m.addAttribute("offsets", offsetService.queryAllOffsetById(employeeId));
        return "lsq/sendSalary/selectOneSum";
    }

    //跳页面->指定年月个人详情薪资组成（识别码，姓名，拿数据）
    @RequestMapping("/showLastOneCountSalary")
    public String showLastOneCountSalary(String name,YearAndMonth key, ModelMap m) {
        System.out.println("控制层！！！！！！！！！"+name+"-----"+key);
        //姓名
        m.addAttribute("name", name);
        //年月、识别码
        m.addAttribute("key",key);
        //指定年月汽车订单
        if ("1".equals(key.getIdentificationCode()))
            m.addAttribute("cars", sendSalaryService.queryLastCarById(key));
            //指定年月修理订单
        else if ("3".equals(key.getIdentificationCode()))
            m.addAttribute("services", sendSalaryService.queryLastServiceById(key));
        //本月报销补贴
        m.addAttribute("offsets", offsetService.queryLastOffsetById(key));
        return "lsq/sendSalary/selectOneLastSum";
    }

    //结算工资（存入数据库）
    @RequestMapping("/checkNowMoney")
    @ResponseBody
    public String checkNowMoney(String employeeId, String ident) {
        //检测是否已结算
        int date = sendSalaryService.queryHasOneById(employeeId);
        if (date == 1)
            return "450";
        //查询结算的人员详细信息(ID和部门识别码)
        EmpSalary empSalary = sendSalaryService.queryOneInfoById(employeeId, ident);
        //结算
        int i = sendSalaryService.insertEmpSalary(empSalary);
        if (i != 0)
            return "200";
        return "500";
    }

}
